package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.OrderSettingList;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service(version = "1.0.0", interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Resource
    private OrderSettingListDao orderSettingListDao;
    @Resource
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(OrderSettingList orderSettingList,String name) {
        Integer count = orderSettingListDao.findByPhoneNumberOrFileNumber(orderSettingList);
        if (count > 0) {
            throw new BusinessException("已存在相同档案号或手机号的预约信息，添加失败");
        }
        orderSettingList.setType(name);
        orderSettingListDao.add(orderSettingList);
    }



    @Override
    public void update(OrderSettingList orderSettingList) {
        Integer count = orderSettingListDao.findByPhoneNumberOrFileNumber(orderSettingList);
        if (count > 0) {
            throw new BusinessException("已存在相同档案号或手机号的预约信息，添加失败");
        }
        orderSettingListDao.update(orderSettingList);


    }

    @Override
    public void delete(Integer id,Date date) {
        orderSettingListDao.delete(id);
            orderSettingDao.reservations(date);
    }

    @Override
    public OrderSettingList findById(int id) {
        OrderSettingList orderSettingList = orderSettingListDao.findById(id);
        //传给预约设置

        return orderSettingList;
    }

    @Override
    public List<OrderSettingList> findAll() {
        return orderSettingListDao.findAll();
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<OrderSettingList> page = orderSettingListDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void updateReservation(Date date) {
        orderSettingDao.reservations(date);
        orderSettingListDao.updateReservations(date);
    }
}
